package com.lavegue.webaction;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.commons.io.FileUtils;

import com.lavegue.common.Constants;
import com.lavegue.common.ListAction;
import com.lavegue.model.Comment;
import com.lavegue.service.CommentService;
import com.opensymphony.xwork2.ActionContext;


/**
 * author: tianzhifen
 * date:2011-12-24
 * description:
 *
 */
public class CommentAction extends ListAction<Comment> {
	
     private CommentService commentService;
	 private String title;//查询使用的标题
	 private int delId;
	 private int detailId;
	 private Comment detailCom=null;//详细信息
	 private String ctitle;//标题
	 private String neirong;//内容
	 private File file;
	 private String fileContentType;
	 private String compangName;
	 private String uName;
	 private int typeId=-1;
	
	
	  //前往公司公告列表
	 public String listCommetnByCompany() {
		 if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		   {
			   return "lg";
		   }
		 pageObj=commentService.getAllCommentByType(pageNo, pageSize,title,Comment.TYPE_GONGGAO);
		return "listCompany";
	 }
	 
	 //删除公告
	 public String delCommentByCompany(){
		 Comment comment=commentService.findById(delId);
		    if(comment!=null){
		    	commentService.deleteById(delId);
		    }
			return listCommetnByCompany();
	 }
	  
	 //点击编辑读取公告数据
	 public String detailCompany()
	 {
		 detailCom=commentService.findById(detailId);
		 if(typeId==Comment.TYPE_GONGGAO)
		 {
			 return "indexDelCom";
		 }
		 return "delCom";
	 }
	 
	 //更新公司公告
	 public String updateCompany()
	 {
		 Comment upCom=commentService.findById(detailId);
		 upCom.setTitle(ctitle);
		 upCom.setContent(neirong);
		 upCom.setCommentTime(new Date());
		 commentService.update(upCom);
		 return listCommetnByCompany();
	 }
	 
	 //新增公司公告
	 public String addCompany()
	 {
		 Comment addCom=new Comment();
		 addCom.setTitle(ctitle);
		 addCom.setContent(neirong);
		 addCom.setCommentTime(new Date());
		 addCom.setType(Comment.TYPE_GONGGAO);
		 commentService.save(addCom);
		 return listCommetnByCompany();
	 }
	 
	 //查询所有的媒体报道
	 public String listMeiti()
	 {
		 if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		   {
			   return "lg";
		   }
		 pageObj=commentService.getAllCommentByType(pageNo, pageSize,title,Comment.TYPE_BAODAO);
		 return "listMT";
	 }
	 
	 
	 //删除媒体报道
	 public String delMeiti()
	 {
		 Comment comment=commentService.findById(delId);
		    if(comment!=null){
		    	commentService.deleteById(delId);
		    }
		    
		   return listMeiti();
	 }
	 
	//点击编辑读取媒体报道数据
	 public String detailMeiti()
	 {
		 detailCom=commentService.findById(detailId);
		 if(typeId==Comment.TYPE_BAODAO)
		 {
			 return "indexDelMtbl";
		 }
		 return "delMeiti";
	 }
	 
	 //新增媒体报道
	 public String addMeiti()
	 {
		 Comment addCom=new Comment();
		 addCom.setTitle(ctitle);
		 addCom.setContent(neirong);
		 addCom.setCommentTime(new Date());
		 addCom.setType(Comment.TYPE_BAODAO);
		 commentService.save(addCom);
		 return listMeiti();
	 }
	 
	 
	 //更新媒体报道
	 public String updateMeiti()
	 {
		 Comment upCom=commentService.findById(detailId);
		 upCom.setTitle(ctitle);
		 upCom.setContent(neirong);
		 upCom.setCommentTime(new Date());
		 commentService.update(upCom);
		 return listMeiti();
	 }
	 
	 
	 
	 
	 //查询所有的帮助中心
	 public String listHelpCenter()
	 {
		 if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		   {
			   return "lg";
		   }
		 pageObj=commentService.getAllCommentByType(pageNo, pageSize,title,Comment.TYPE_HELP);
		 return "listHelp";
	 }
	 
	  //删除帮助中心
	 public String delHelpCenter()
	 {
		 Comment comment=commentService.findById(delId);
		    if(comment!=null){
		    	commentService.deleteById(delId);
		    }
		   return listHelpCenter();
	 }
	 
	//点击编辑读取帮助中心数据
	 public String detailHelpCenter()
	 {
		 detailCom=commentService.findById(detailId);
		 if(typeId==Comment.TYPE_HELP)
		 {
			 return "indexDelHelp";
		 }
		 return "delHelp";
	 }
	//新增帮助中心
	 public String addHelpCenter()
	 {
		 Comment addCom=new Comment();
		 addCom.setTitle(ctitle);
		 addCom.setContent(neirong);
		 addCom.setCommentTime(new Date());
		 addCom.setType(Comment.TYPE_HELP);
		 commentService.save(addCom);
		 return listHelpCenter();
	 }
	 
	 
	 //更新帮助中心
	 public String updateHelpCenter()
	 {
		 Comment upCom=commentService.findById(detailId);
		 upCom.setTitle(ctitle);
		 upCom.setContent(neirong);
		 upCom.setCommentTime(new Date());
		 commentService.update(upCom);
		 return listHelpCenter();
	 }
	 
	 
	 //查询所有的为什么选择易彩通
	 public String listWhyChooes()
	 {
		 if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		   {
			   return "lg";
		   }
		 pageObj=commentService.getAllCommentByType(pageNo, pageSize,title,Comment.TYPE_WHY);
		 return "listWhyCh";
	 }
	 
	  //删除为什么选择易彩通
	 public String delWhyChooes()
	 {
		 Comment comment=commentService.findById(delId);
		    if(comment!=null){
		    	commentService.deleteById(delId);
		    }
		   return listWhyChooes();
	 }
	 
	//点击编辑读取为什么选择易彩通数据
	 public String detailWhyChooes()
	 {
		 detailCom=commentService.findById(detailId);
		 if(typeId==Comment.TYPE_WHY)
		 {
			 return "indexDelyct";
		 }
		 return "delWhyChooes";
	 }
	//新增为什么选择易彩通
	 public String addWhyChooes()
	 {
		 Comment addCom=new Comment();
		 addCom.setTitle(ctitle);
		 addCom.setContent(neirong);
		 addCom.setCommentTime(new Date());
		 addCom.setType(Comment.TYPE_WHY);
		 commentService.save(addCom);
		 return listWhyChooes();
	 }
	 
	 
	 //更新为什么选择易彩通
	 public String updateWhyChooes()
	 {
		 Comment upCom=commentService.findById(detailId);
		 upCom.setTitle(ctitle);
		 upCom.setContent(neirong);
		 upCom.setCommentTime(new Date());
		 commentService.update(upCom);
		 return listWhyChooes();
	 }
	 
	 
	//查询所有的用户评价
	 public String listUserComent()
	 {
		 if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		   {
			   return "lg";
		   }
		 pageObj=commentService.getAllCommentByType(pageNo, 5,title,Comment.TYPE_USER_COMMENT);
		 return "listUserC";
	 }
	 
	 //删除用户评价
	 public String delUserComent()
	 {
		 Comment comment=commentService.findById(delId);
		    if(comment!=null){
		    	commentService.deleteById(delId);
		    }
		   return listUserComent();
	 }
	 
	//点击编辑读取用户评价
	 public String detailUserComent()
	 {
		 detailCom=commentService.findById(detailId);
		 if(typeId==Comment.TYPE_USER_COMMENT)
		 {
			 return "indexDelyhpj";
		 }
		 return "delUserComent";
	 }
	 
	 //更新用户评价
	 public String updateUserComent()
	 {
		 Comment upCom=commentService.findById(detailId);
		 upCom.setTitle(ctitle);
		 upCom.setContent(neirong);
		 upCom.setCommentTime(new Date());
		 upCom.setCompany(compangName);
		 upCom.setUserName(uName);
		
		 if(file!=null)
		 {
		 String picName=String.valueOf(System.currentTimeMillis());
		 try {
			 String stype=fileContentType.substring(fileContentType.indexOf('/')+1);
			 File f = new File(this.getProjectPath() +"/upload/usercoment_pic/"+picName+"."+stype);
			 f.createNewFile();
			FileUtils.copyFile(file,f);
			upCom.setIco(picName+"."+stype);
		} catch (IOException e) { 
			LOG.error("上传文件复制异常:", e);
		}
		 
		 }
		 commentService.update(upCom);
		 
		 return listUserComent();
	 }
	 
	//新增用户评价
	 public String addUserComent()
	 {
		 Comment addCom=new Comment();
		 addCom.setTitle(ctitle);
		 addCom.setContent(neirong);
		 addCom.setCommentTime(new Date());
		 addCom.setType(Comment.TYPE_USER_COMMENT);
		 addCom.setCompany(compangName);
		 addCom.setUserName(uName);
		 if(file!=null)
		 {
		 String picName=String.valueOf(System.currentTimeMillis());
		 try {
			 String stype=fileContentType.substring(fileContentType.indexOf('/')+1);
			 File f = new File(this.getProjectPath() +"/upload/usercoment_pic/"+picName+"."+stype);
			 f.createNewFile();
			FileUtils.copyFile(file,f);
			addCom.setIco(picName+"."+stype);
		} catch (IOException e) { 
			LOG.error("上传文件复制异常:", e);
		}
		 
		 }
		 commentService.save(addCom);
		 return listUserComent();
	 }
	 
	 
	 
	 
	 public String lisComment()
	 {
		 pageObj=commentService.getAllCommentByType(pageNo, pageSize,null,typeId);
		 String result="";
		 
		 if(typeId==Comment.TYPE_BAODAO)
			    result="listbaodao";
		 if(typeId==Comment.TYPE_GONGGAO)
			    result="listgonggao";
		 if(typeId==Comment.TYPE_HELP)
			    result="listIndexhelp";
		 if(typeId==Comment.TYPE_USER_COMMENT)
			    result="listyhpj";
		 if(typeId==Comment.TYPE_WHY)
			    result="listwhy";
		 ActionContext.getContext().getValueStack().set("typeId", 0);
		 return result;
	 }
	 
	

	public CommentService getCommentService() {
		return commentService;
	}

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	
	
	public int getDelId() {
		return delId;
	}

	public void setDelId(int delId) {
		this.delId = delId;
	}

	public String getTitle() {
		return title;
	}

	public String getCtitle() {
		return ctitle;
	}

	public void setCtitle(String ctitle) {
		this.ctitle = ctitle;
	}

	public String getNeirong() {
		return neirong;
	}

	public void setNeirong(String neirong) {
		this.neirong = neirong;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getDetailId() {
		return detailId;
	}

	public void setDetailId(int detailId) {
		this.detailId = detailId;
	}

	public Comment getDetailCom() {
		return detailCom;
	}

	public void setDetailCom(Comment detailCom) {
		this.detailCom = detailCom;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getCompangName() {
		return compangName;
	}

	public void setCompangName(String compangName) {
		this.compangName = compangName;
	}

	public String getuName() {
		return uName;
	}

	public void setuName(String uName) {
		this.uName = uName;
	}

	public int getTypeId() {
		return typeId;
	}

	public void setTypeId(int typeId) {
		this.typeId = typeId;
	}
	 
	 
	 

	
}

